package com.koron.css2.clientCenter.impl;

import com.koron.css2.clientCenter.Enum.ProcessStateEnum;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserInfoMerge;
import com.koron.css2.clientCenter.bean.UserInfoMergeDetail;
import com.koron.css2.clientCenter.mapper.*;
import com.koron.css2.clientCenter.queryBean.UserInfoMergeDetailQuery;
import com.koron.css2.clientCenter.queryBean.UserInfoMergeQuery;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.workflow.AbstractProcessInterface;
import com.koron.css2.workflow.ProcessInterface;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 并户-工作流提交成功-回调接口
 *
 * @author 朱超
 * @since 2021-12-31 15:15:08
 */
@Service
public class UserInfoMergeProcess extends AbstractProcessInterface {

    @Override
    public MessageBean<?> before(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
        try {
            UserInfoMergeMapper userInfoMergeMapper = factory.getMapper(UserInfoMergeMapper.class);
            UserInfoMergeDetailMapper userInfoMergeDetailMapper = factory.getMapper(UserInfoMergeDetailMapper.class);
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);

            // 1. 判断是否通过
            if (operation == ProcessInterface.OPERATION_AGREE) {
                // 2. 根据id查询单条数据
                UserInfoMergeQuery userInfoMergeQuery = new UserInfoMergeQuery();
                userInfoMergeQuery.setId(billId);
                UserInfoMerge userInfoMerge = userInfoMergeMapper.getSingle(userInfoMergeQuery);
                // 3. 查询并户详情
                UserInfoMergeDetailQuery userInfoMergeDetailQuery = new UserInfoMergeDetailQuery();
                userInfoMergeDetailQuery.setMergeCode(userInfoMerge.getBillNo());
                List<UserInfoMergeDetail> userInfoMergeDetailList = userInfoMergeDetailMapper.getList(userInfoMergeDetailQuery);
//                // 3.1 校验：一个账户不能对应多个客户
//                try {
//                    userInfoMergeDetailList.stream().collect(Collectors.toMap(UserInfoMergeDetail::getAccountNo, UserInfoMergeDetail::getCtmNo));
//                } catch (Exception e) {
//                    return MessageBean.create(Constant.MESSAGE_INT_FAIL,"一个账户仅可绑定一个客户！请修改！",null);
//                }
                // 判断是否是审批阶段
                if (fromNodeCode.equals(ProcessStateEnum.PROCESSING.getState())) {
                    // 4. 更新之前查询原始的originalCtmNoSet
                    List<String> originalCtmNoList = userInfoMapper.getCtmNoListByCertNo(userInfoMerge.getCertNo());
                    // 5. user_info表：根据userNo批量更新更新ctmNo，accountNo，ctmName，ctmAddr,certNo,certType,likeTel,mobile,faxNumber
                    // 5.1 certNo,certType,likeTel,mobile,faxNumber
                    for (UserInfoMergeDetail userInfoMergeDetail: userInfoMergeDetailList) {
                        CtmInfoBean ctmInfoBean = ctmInfoMapper.getCtmInfoByCtmNo(userInfoMergeDetail.getCtmNo(),CtmInfoBean.CTM);
                        userInfoMergeDetail.setCertNo(ctmInfoBean.getCertNo());
                        userInfoMergeDetail.setCertType(ctmInfoBean.getCertType());
                        userInfoMergeDetail.setLinkTel(ctmInfoBean.getLinkTel());
                        userInfoMergeDetail.setMobile(ctmInfoBean.getMobile());
                        userInfoMergeDetail.setFaxNumber(ctmInfoBean.getFaxNumber());
                    }
                    userInfoMapper.batchUpdateCtmInfoAndAccountNo(userInfoMergeDetailList);
                    // 6. 批量查出ctmNo对应的accountNoCount
                    Map<String, Map> map = userInfoMapper.getAccountNoCountByCtmNoList(originalCtmNoList);
                    // 6.1 逐个更新数据
                    for (String ctmNo : originalCtmNoList) {
                        // 如果查出来还有这个ctmNo
                        if (map.get(ctmNo) != null) {
                            ctmInfoMapper.updateStatusByCtmNo(ctmNo, 1);
                        } // 如果没有，说明ctmNo对应的accountNo为空
                        else {
                            ctmInfoMapper.updateStatusByCtmNo(ctmNo, 2);
                        }
                    }
                    // 7. 更新user_account表中的ctm_no和account_no的关系
                    accountInfoMapper.batchUpdateCtmNoByAccountNo(userInfoMergeDetailList);
                }
            }

            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "并户提交成功", void.class);
        } catch (Exception e) {
            logger.error("并户提交失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "并户提交失败", void.class);
        }
    }

}
